﻿using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;

namespace Eg.FluentMappings
{
  

    public  class Entity<Tid>
    {
        public virtual Tid Id { get; protected set; }
        public virtual int Version { get; set; }

        public override bool Equals(Object obj)
        {
            return Equals(obj as Entity<Tid>);
        }
        private static bool IsTransient(Entity<Tid> obj)
        {
            return obj != null &&
                Equals(obj.Id, default(Tid));
        }
        private Type GetUnproxiedType()
        {
            return GetType();
        }
        public virtual bool Equals(Entity<Tid> other)
        {
            if (other == null)
                return false;
            if (ReferenceEquals(this, other))
                return true;
            if (!IsTransient(this) &&
                !IsTransient(other) &&
                Equals(Id, other.Id))
            {
                var otherType = other.GetUnproxiedType();
                var thisType = GetUnproxiedType();
                return thisType.IsAssignableFrom(otherType) ||
                       otherType.IsAssignableFrom(thisType);
            }
            return false;
        }

        public override int GetHashCode()
        {
            if (Equals(Id, default(Tid)))
                return base.GetHashCode();
            return Id.GetHashCode();
        }
    
    }

    public abstract class Entity : Entity<Guid>
    {
    }

}
